﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web.Routing;
using System.Web.Mvc;
using System.Web.Http;

using ERPStore.Web.Routing;

using Microsoft.Practices.Unity;
using Microsoft.AspNet.SignalR;

using ERPStore.Extensions;

using EventBrokR;

namespace ERPStore.Diagnostics
{
	public class PluginInit : ERPStore.Extensibility.PluginInit
	{
		public PluginInit()
		{
			Container = GlobalConfiguration.Configuration.DependencyResolver.GetContainer();
		}

		Microsoft.Practices.Unity.IUnityContainer Container { get; set; }

		public override void AddServices()
		{
			Container.RegisterType<CloudSoft.Repositories.IDbContextFactory<Repositories.DiagnosticDbContext>, CloudSoft.Repositories.DbContextFactory<Repositories.DiagnosticDbContext>>(new ContainerControlledLifetimeManager());
			Container.RegisterType<CloudSoft.Repositories.IRepositoryInitializer, CloudSoft.Repositories.Initializers.SqlSchemaInitializer<Repositories.DiagnosticDbContext>>("diagnostischemainitializer", new ContainerControlledLifetimeManager());
			Container.RegisterType<CloudSoft.Repositories.IRepository<Repositories.DiagnosticDbContext>, CloudSoft.Repositories.SqlRepository<Repositories.DiagnosticDbContext>>(new ContainerControlledLifetimeManager());

			var eb = Container.Resolve<EventBrokR.IPublisher>();
			eb.Container.Register<EventConsumers.EndApplicationRequestedEventConsumer>();
			eb.Container.Register<EventConsumers.ImageNotFoundEventConsumer>();
			eb.Container.Register<EventConsumers.PageNotFoundEventConsumer>();

			AutoMapper.Mapper.CreateMap<Models.RewriteUrl, Repositories.Datas.RewriteUrlData>()
				.ForMember(dest => dest.Destination, opt => opt.MapFrom(source => source.DestinationUrl))
				.ForMember(dest => dest.Source, opt => opt.MapFrom(source => source.SourceUrl));

			AutoMapper.Mapper.CreateMap<Repositories.Datas.RewriteUrlData, Models.RewriteUrl>()
				.ForMember(dest => dest.DestinationUrl, opt => opt.MapFrom(source => source.Destination))
				.ForMember(dest => dest.SourceUrl, opt => opt.MapFrom(source => source.Source));

			Container.RegisterType<ERPStore.Services.IPersistentConnection, Services.DiagnosticConnection>();
		}

		public override void RegisterRoutes()
		{
			base.RegisterRoutes();

			var namespaces = new string[] { "ERPStore.Diagnostics.Controllers" };

			RouteTable.Routes.MapERPStoreRoute(
				"DiagnosticFeedback"
				, "diagnostic/feedback/showform"
				, new { controller = "Feedback", action = "ShowForm" }
				, namespaces
			);

			RouteTable.Routes.MapRoute(
				"Diagnostic"
				, "diagnostic/{action}/{*id}"
				, new { controller = "Diagnostic", action = "Index", id = string.Empty }
				, namespaces
			);

			RouteTable.Routes.MapHttpRoute(
				"DiagnosticApi"
				, "api/diagnostic/{action}/{*id}"
				, new { controller = "DiagnosticApi", action = "Ping", id = UrlParameter.Optional }
			);

		}

		public override void Load()
		{
			base.Load();
			var metaDefinitionService = Container.Resolve<ERPStore.Web.Services.IModelMetadataRegistrationService>();

			metaDefinitionService.Register(new Models.FeedbackMetaDefinition());

			var initializer = Container.Resolve<CloudSoft.Repositories.IRepositoryInitializer>("diagnostischemainitializer");
			initializer.Initialize("_erpstore_diagnostics_schema");
		}

	}
}
